package cloud.tianai.nio.channel;

import cloud.tianai.nio.handler.ChannelPipeline;
import cloud.tianai.nio.loop.EventLoop;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;

/**
 * @Author: 天爱有情
 * @date 2023/6/20 14:49
 * @Description 管道
 */
public interface Channel {
    /**
     * 绑定事件持续处理器
     *
     * @param eventLoop
     */
    void register(EventLoop eventLoop);

    /**
     * 获取事件持续处理器
     *
     * @return
     */
    EventLoop getEventLoop();

    /**
     * 通道内部的管道
     *
     * @return
     */
    ChannelPipeline getPipeline();

    /**
     * 绑定地址
     *
     * @param address address
     */
    void bind(SocketAddress address) throws IOException;

    /**
     * 连接
     *
     * @param address address
     * @throws IOException
     */
    void connect(SocketAddress address) throws IOException;

    /**
     * 是否已连接
     *
     * @return boolean
     */
    boolean isConnect();

    /**
     * 开始读取，nio的实现即注册感兴趣的事件
     */
    void listen();

    void write(ByteBuffer buffer) throws IOException;

    void write(Object obj) throws IOException;

    void close();
}
